<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% user_name = ((params[:hide_student_name] && !params[:hide_student_name].to_i.zero?) || (@quiz.survey? && @quiz.anonymous_submissions)) ? t(:anonymous_username, "Student") : context_user_name(@context, @user) %>
<% user_name = t(:logged_out_user, "Logged Out User %{index}", :index => @logged_out_user_index) if !user_name && !@submission.user_id %>
<% content_for :page_title, (@quiz.survey? ?
     join_title(t(:page_title_survey, "%{user}'s Survey History", :user => user_name), @quiz.quiz_title || t(:default_survey_name, "Survey %{course_name}", :course_name => @context.name)) :
     join_title(t(:page_title_quiz, "%{user}'s Quiz History", :user => user_name), @quiz.quiz_title || t(:default_quiz_name, "Quiz %{course_name}", :course_name => @context.name)))
%>

<% quizNavClass = 'quiz-nav' %>
<% unless params[:headless] %>
  <% quizNavClass = 'quiz-nav-fullpage' %>
<% end %>

<% if @current_user.preferences[:enable_speedgrader_grade_by_question] %>
<div class='<%= quizNavClass %> pagination'>
  <ul class='nav'>
    <li class='nav-arrow' id='nav-prev'>
      <a href='#' id='nav-link-prev' title='{{#t}}Previous questions{{/t}}'>&lt;</a>
    </li>
    <li>
      <div id='quiz-nav-inner-wrapper'>
        <ul>
          <% @submission.questions_as_object.each_with_index do |question, i| %>
            <% index = i + 1 %>
            <li id='quiz_nav_<%= question['id'] %>' class="q<%= index %> quiz-nav-li" data-id='<%= question['id'] %>' data-index='<%=i%>'>
              <a class='question_<%= question['id'] %> question-nav-link' data-id='<%= question['id'] %>' href="#question_<%= question['id'] %>">
                <%= index %>
              </a>
            </li>
          <% end %>
        </ul>
      </div>
    </li>
    <li class='nav-arrow' id='nav-next'>
      <a href='#' id='nav-link-next' title='{{#t}}Next questions{{/t}}'>&gt;</a>
    </li>
  </ul>
</div>
<div class=quizzes-speedgrader-padding></div>
<% if @quiz.quiz_groups.exists? %>
<div class="grade-by-question-warning">
  {{#t}}The grade by question feature is affected by questions that are shuffled in a group.{{/t}}
</div>
<% end %>
<% end %>

<% unless params[:headless] %>
  <% content_for :right_side do %>
    <% if @current_version && can_do(@quiz, @current_user, :grade) %>
      <p>
        <% if @quiz.survey? %>
          <%= t(:latest_survey_results, "Here's the latest survey results for %{user}. You can modify the points for any question and add more comments, then click \"Update Scores\" at the bottom of the page.", :user => user_name) %>
        <% else %>
          <%= t(:latest_quiz_results, "Here's the latest quiz results for %{user}. You can modify the points for any question and add more comments, then click \"Update Scores\" at the bottom of the page.", :user => user_name) %>
        <% end %>
      </p>
    <% end %>
    <% unless params[:headless] %>
        <h2>
          <% if @quiz.survey? %>
            {{#t}}Survey Submissions{{/t}}
          <% else %>
            {{#t}}Quiz Submissions{{/t}}
          <% end %>
        </h2>
      <div>
        <ul id="quiz_versions" style="max-height: 250px; overflow: auto;">
        <% @version_instances.each do |version_instance| %>
          <li class="quiz_version <%= "selected" if version_instance.version_number == @submission.version_number %>">
            <a class="no-hover" href="?version=<%= version_instance.version_number %><%= "&user_id=#{@submission.user_id}" if @current_user.id != @submission.user_id %>">
              <% if version_instance.pending_review? %>
                <% needs_review_title = t('titles.submission_needs_review', "This submission needs review") %>
                <% if can_do(@quiz, @current_user, :grade) %>
                  <i class="icon-warning" title="<%= needs_review_title %>"><span class="screenreader-only"><%= needs_review_title %></span></i>
                <% else %>
                  <% pending_review_title = t('titles.score_pending_review', "This score is pending review, and may change") %>
                  <i class="icon-hour-glass" title="<%= pending_review_title %>"><span class="screenreader-only"><%= pending_review_title %></span></i>
                <% end %>
              <% end %>
              <%= t(:score_for_attempt, "Attempt %{attempt_number}: %{score}", :attempt_number => version_instance.attempt, :score => render_score(version_instance.score)) %>
            </a>
          </li>
        <% end %>
        </ul>
        <div style="margin-bottom: 10px;">
          <% if !@quiz.unlimited_attempts? %>
            <% if @current_submission.attempts_left <= 0 %>
              <%= t(:no_more_attempts, "%{user} has no attempts left", :user => user_name) %>
            <% else %>
              <%= t(:count_attempts_left, {:one => "%{user} has 1 attempt left", :other => "%{user} has %{count} attempts left"}, :user => user_name, :count => @current_submission.attempts_left) %>
            <% end %>
            <% if can_do(@submission, @current_user, :add_attempts) %>
              <%= form_for :quiz_submission, :url => context_url(@context, :context_quiz_extensions_url, @quiz.id, @submission.user_id), :html => {:method => :post} do |f| %>
                <input type="hidden" name="extra_attempts" value="<%= (@current_submission.extra_attempts || 0) + 1 %>"/>
                <button type="submit" class="btn">{{#t}}allow this student an extra attempt{{/t}}</button>
              <% end %>
            <% end %>
          <% else %>
            <% if @quiz.survey? %>
              {{#t}}This survey has unlimited attempts{{/t}}
            <% else %>
              {{#t}}This quiz has unlimited attempts{{/t}}
            <% end %>
          <% end %>
        </div>
        <a href="<%= context_url(@context, :context_quiz_url, @quiz) %>" class='no-hover icon-arrow-left'> <%= @quiz.survey? ? t('links.back_to_survey', "Back to Survey") : t('links.back_to_quiz', "Back to Quiz") %></a>
      </div>
    <% end %>
  <% end %>
<% end %>

<header class="quiz-header">
  <h2>
    <% if @quiz.quiz_title %>
      <%= t(:named_quiz_results, "%{quiz_name} Results for %{user}", :quiz_name => @quiz.quiz_title, :user => user_name) %>
    <% elsif @quiz.survey? %>
      <%= t(:unnamed_survey_results, "Survey Results for %{user}", :user => user_name) %>
    <% else %>
      <%= t(:unnamed_quiz_results, "Quiz Results for %{user}", :user => user_name) %>
    <% end %>

    <% if @context.feature_enabled?(:quiz_log_auditing) && can_do(@submission, @current_user, :view_log)%>
      <%= link_to t(:view_log, 'View Log'),
        course_quiz_quiz_submission_events_url(@context, @quiz, @submission),
        { "class" => "pull-right" }
      %>
    <% end %>
  </h2>
</header>
<div id="feel_free_to_toggle_message">
</div>
<% if !@submission || @submission.settings_only? %>
  <% if @quiz.survey? %>
    {{#t}}Survey hasn't been taken yet.{{/t}}
  <% else %>
    {{#t}}Quiz hasn't been taken yet.{{/t}}
  <% end %>
<% elsif @submission.untaken? %>
  <% if @quiz.survey? %>
    {{#t}}Survey hasn't been taken yet or is currently in progress{{/t}}
  <% else %>
    {{#t}}Quiz hasn't been taken yet or is currently in progress{{/t}}
  <% end %>
<% else %>
  <%= form_tag context_url(@context, :context_quiz_quiz_submission_url, @quiz.id, @submission.id), {:method => :put, :id => "update_history_form"} do %>
    <input type="hidden" name="override_scores" value="true"/>
    <input type="hidden" name="hide_student_name" value="<%= params[:hide_student_name] || 0 %>"/>

    <% if params[:headless] %>
      <input type="hidden" name="headless" value="1"/>
    <% end %>
    <input type="hidden" name="submission_version_number" value="<%= @submission.version_number %>"/>
    {{> views/quiz_submission}}
  <% end %>
<% end %>

<div style="display: none;" id="submission_details">
  <div class="version_number"><%= @version_number.to_s %></div>
  <div class="user_id"><%= @submission.user_id %></div>
</div>

<% js_bundle :quiz_history %>
